<template>
    <Header></Header>
    <Banner :isIndex="false" @changeCourse="changeCourse" :bannerList="banner_list">
        <template #freeSelect>
<!--            {{freeSelectList}}-->
            <el-select v-if="freeSelectList.length > 0" v-model="activeFree" class="m-2" placeholder="国际薪税师">
                <el-option
                        v-for="item in freeSelectList"
                        :key="String(item.id)"
                        :label="item.name"
                        :value="String(item.id)"
                />
                <template #prefix>
                    <span style="color: rgba(2, 122, 213, 1)">当前课程</span>
                </template>
            </el-select>
        </template>
        <template #search>
            <el-input
                    v-model="search"
                    style="width:220px"
                    placeholder="请输入关键字"
                    :suffix-icon="Search"
                    @input="searchInput"
            />
        </template>
    </Banner>
    <div class="question-body">
        <div class="breadcrumb">
            <el-breadcrumb :separator-icon="'ArrowRightBold'">
                <el-breadcrumb-item class="link" @click="$router.push({name:'index'})">首页</el-breadcrumb-item>
                <el-breadcrumb-item>{{activeName}}</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <el-row justify="space-between">
            <el-col :span="4">
                <el-menu
                        active-text-color="white"
                        background-color="rgba(235, 244, 255, 1)"
                        class="question"
                        :default-active="currentItem"
                        text-color="rgba(102, 102, 102, 1)"
                        @select="handleSelect"
                >
                    <el-menu-item index="freeVideo">
                        <el-icon><setting /></el-icon>
                        <span>免费试听</span>
                    </el-menu-item>
                </el-menu>
            </el-col>
            <el-col :span="19">
<!--                {{typeof currentItem}}-->
                <component :is="currentItem" :activeFree="activeFree" :subject="subject" :currentItem="currentItem" :subject_id="activeSubjectId" :subject_name="activeSubjectName"></component>
            </el-col>
        </el-row>
    </div>
    <staticFooter></staticFooter>
</template>

<script>
    import staticFooter from "../../../components/staticFooter";
    import {throttle} from "../../../utils/debounce/debounce";
    import { Search } from '@element-plus/icons-vue'
    import myCourse from "./tabs/myCourse";
    import freeVideo from "./tabs/freeVideo";
    import Banner from "../../../components/Banner";
    import Header from "@/components/Header";
    import {useRoute, useRouter} from "vue-router";
    import {bannerList, complexSearch, courseList, videoFreeCourse,types} from "../../../api/api";
    import {reactive,ref,onMounted } from 'vue';
    export default {
        name: "index",
        setup(){

            let router = useRouter();
            let route = useRoute();
            //右侧动态组件
            let currentItem = ref('myCourse');

            let subject = ref(0);
            subject.value = Number(route.params.subject_id)


            //当前课程
            let activeSubjectId = ref(0)
            let activeSubjectName = ref('')
            let activeName = ref('')
            let active_map = {'我的课程':'myCourse','免费试听':'freeVideo'};
            currentItem.value = active_map[route.params.activeName];
            activeName.value = route.params.activeName;
            activeSubjectId.value = localStorage.getItem('activeCourse')
            activeSubjectName.value = localStorage.getItem('activeCourseName')

            //banner图集合
            let banner_list = ref([])
            let getBannerList = () =>{
                bannerList({id:activeSubjectId.value,type:1}).then(res =>{
                    console.log('轮播图列表',res)
                    banner_list.value = res.data
                })
            }
            getBannerList();

            let activeFree = ref(0);
            let freeSelectList = ref([]);
            let getVideoFreeCourse = () =>{
                videoFreeCourse().then(res =>{
                    console.log('免费试听课程列表',res)
                    freeSelectList.value = res.data
                    activeFree.value = String(res.data[0].id)
                })
            }
            getVideoFreeCourse();

            //切换课程
            let changeCourse = (activeCourse) => {
                let activeCourseName = course_list.filter(item => item.courseID == activeCourse.courseId)[0].name;
                console.log('activeCourseName',activeCourseName)
                window.localStorage.setItem('activeCourseName', String(activeCourseName));
            }

            //课程列表
            let course_list = reactive([]);
            const getCourseList = () =>{
                courseList().then(res =>{
                    console.log('课程列表',res)
                    course_list.push(...res.data)
                    let acoutCourse = window.localStorage.getItem('activeCourse') ? window.localStorage.getItem('activeCourse'):course_list[0].courseID
                    window.localStorage.setItem('activeCourse',acoutCourse);
                })
            }
            getCourseList();

            //左侧menu
            const handleSelect = (key,keyPath) => {
                currentItem.value = key
            }

            let search = ref('');
            let searchInput = (value) =>{
                complexSearch({courseID:activeFree.value,content:'薪税师',type:''}).then(res =>{
                    console.log(res);
                })
            }

            onMounted(() =>{
                // let current = localStorage.getItem('currentItem')
                // console.log('current',current)
                // if(current != 'undefined' || current != 'null'){
                //   console.log('current',current)
                //   currentItem.value = current
                // }else{
                //   currentItem.value = 'myCourse'
                // }
            })
            return{
                subject,
                searchInput,
                Search,
                search,
                activeFree,
                freeSelectList,
                changeCourse,
                activeName,
                handleSelect,
                currentItem,
                activeSubjectId,
                activeSubjectName,
                course_list,
                banner_list,
            }
        },
        components:{
            Header,
            Banner,
            freeVideo,
            myCourse,
            staticFooter
        }
    }
</script>

<style>
    .el-menu-item.is-active{
        background-color: rgba(42, 130, 228, 1);
    }
</style>

<style scoped>
    .link{
        cursor: pointer;
    }
    .link :hover{
        color: #409eff;
    }
    .question-body{
        width: 1200px;
        margin: 0 auto;
        margin-bottom: 120px;
    }
    .question-body .breadcrumb{
        margin: 20px 0px;
    }
</style>
